/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DTO;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

/**
 *
 * @author NoiR
 */
public class JAXBGenerateJavaObjs {

    // ThuanNH
    public static void main(String[] args) {
        // JAXBBinding("song.xsd", "song"); // ThuanNH
        // JAXBBinding("user.xsd", "user"); // TungNT
        // JAXBBinding("artist.xsd", "artist"); // TanND
        // JAXBBinding("song.xsd", "song"); // ThuanNH
        // JAXBBinding("user.xsd", "user"); // TungNT
        // JAXBBinding("genre.xsd", "genre"); // TanND
        // JAXBBinding("album.xsd", "album"); // DinhNP
        // JAXBBinding("playlist.xsd", "playlist"); // DinhNP
        JAXBBinding("viewHistory.xsd", "viewHistory"); // DinhNP
    }

    // ThuanNH
    private static void JAXBBinding(String fileName, String outputPackage) {
        try {
            String output = "src/java/DTO";
            String schemaFolder = "src/java/schema/";

            SchemaCompiler sc = XJC.createSchemaCompiler(); //create schema compiler
            sc.setErrorListener(new com.sun.tools.xjc.api.ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            sc.forcePackageName(outputPackage);
            File schema = new File(schemaFolder + fileName); // set location of file xsd.
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind(); // binding process

            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output)); // build file object.
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
